package com.yuandian.management.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuandian.api.customer_perception.po.ImportanceOperatorParams;
import com.yuandian.api.customer_perception.vo.ImportanceOperator;
import com.yuandian.api.customer_perception.vo.ImportanceOperatorVo;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.bpm.common.security.util.SecurityUtils;
import com.yuandian.management.mapper.ConfImportanceOperatorMapper;
import com.yuandian.management.service.ConfImportanceOperatorService;
import com.yuandian.utils.BeanUtils;
import com.yuandian.utils.PageDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户感知-重点用户表操作
 * @author EDY
 * @date 2023-3-15
 */
@Service
public class ConfImportanceOperatorServiceImpl extends ServiceImpl<ConfImportanceOperatorMapper, ImportanceOperator>
		implements ConfImportanceOperatorService {

	@Autowired
	private ConfImportanceOperatorMapper confImportanceOperatorMapper;

	/**
	 * 查询重点用户列表
	 *
	 * @param params 业务统计请求参数对象
	 * @return 通用返回对象
	 */
	@Override
	public R<Object> findByPager(ImportanceOperatorParams params) {
		Page<ImportanceOperator> page = new Page<>(params.getPageNum(), params.getPageSize());
		if (StringUtils.isNotEmpty(params.getName())|| StringUtils.isNotEmpty(params.getOperatorId())){
			this.lambdaQuery()
					.like(StringUtils.isNotEmpty(params.getName()), ImportanceOperator::getName, params.getName())
					.like(StringUtils.isNotEmpty(params.getOperatorId()), ImportanceOperator::getOperatorId, params.getOperatorId())
					.orderByAsc(ImportanceOperator::getId)
					.page(page);
			return  R.ok(new PageDataInfo(page.getTotal(), page.getRecords()));
		}

		Map<String, Object> param=new HashMap<>();

		String operatorId = params.getOperatorId();
		String name = params.getName();
		param.put("operatorId", operatorId);
		param.put("name", name);
		Page<ImportanceOperator> objectPage = new Page<>(params.getPageNum(), params.getPageSize());
		List<ImportanceOperator> list = confImportanceOperatorMapper.findByPager(objectPage, param);

		LambdaQueryWrapper<ImportanceOperator> wrapper = new LambdaQueryWrapper<>();
		if (null != operatorId) {
			wrapper.like(ImportanceOperator::getOperatorId, operatorId);
		}
		if (null != name) {
			wrapper.like(ImportanceOperator::getName, name);
		}
		long count = this.count(wrapper);

		return  R.ok(new PageDataInfo(count, list));
	}


	/**
	 * 新增重点用户
	 */
	@Override
	public R<Object> add(ImportanceOperatorVo vo) {
		ImportanceOperator importanceOperator = BeanUtils.convert(vo, ImportanceOperator.class);
		//1. 校验操作员ID是否存在
		int count = Math.toIntExact(this.lambdaQuery().eq(ImportanceOperator::getOperatorId, importanceOperator.getOperatorId()).count());
		if (count > 0) {
			return R.failed("已经存在该操作员");
		}
		//2. 新增
		importanceOperator.setStatus("0");
		importanceOperator.setCreateBy(SecurityUtils.getUser().getName());
		importanceOperator.setUpdateBy(SecurityUtils.getUser().getName());
		importanceOperator.setCreateTime(LocalDateTime.now());
		importanceOperator.setUpdateTime(LocalDateTime.now());
		if (this.save(importanceOperator)) {
			return R.ok("新增成功");
		}
		return R.failed("新增失败");
	}

	/**
	 * 修改重点用户
	 */
	@Override
	public R<Object> updateImportanceOperator(ImportanceOperatorVo vo) {
		ImportanceOperator importanceOperator = BeanUtils.convert(vo, ImportanceOperator.class);
		if (vo.getId() == null || this.getById(vo.getId()) == null) {
			return R.failed("不存在该操作员");
		}
		importanceOperator.setUpdateBy(SecurityUtils.getUser().getName());
		importanceOperator.setUpdateTime(LocalDateTime.now());
		if (this.updateById(importanceOperator)) {
			return R.ok("修改成功");
		}
		return R.failed("修改失败");
	}

}
